﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PhotoGallery.Model;
using PhotoGallery.BLL;
using PhotoGallery.DLL;

public partial class photo : System.Web.UI.Page
{
    public string list1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["pid"] != null)
        {
            if (!IsPostBack)
            {
                string pid = Request["pid"].ToString();
                PhotoAction pa = new PhotoAction();
                Picture p = pa.showPic(pid);
                this.image.ImageUrl = p.Pic;
                this.image.Width = 700;
                //this.iuser.NavigateUrl = "space.aspx?uid=" + p.Uid;
                UserDataAccess uda = new UserDataAccess();
                User u = uda.searchUserById(p.Uid);
                AlbumsDataAccess ada = new AlbumsDataAccess();
                CommentDataAccess cda = new CommentDataAccess();
                Albums a = ada.searchAlbumById(p.Aid);
                Session["album"] = a;
                this.iuser.Text = u.Nickname;
                this.albumlink.Text = a.Title;
                this.albumlink.NavigateUrl = "albumshow.aspx?aid=" + p.Aid;
                //显示评论
                List<Commentcllection> collection = new List<Commentcllection>();
                List<Comment> comments = cda.searchCommentByPicId(pid);
                foreach (Comment m in comments) 
                { 
                    string username=uda.searchUserById(m.Uid).Nickname;
                    collection.Add(new Commentcllection(m.Uid,username,m.Content,m.Time));
                }
                this.rpt_Message.DataSource = collection;              
                this.rpt_Message.DataBind();
                
                List<Picture> piclist = pa.showAllPhotosofAlbum(a.Aid);
                list1 = "";
                foreach (Picture pic in piclist)
                {
                    list1 += "<li><a href='photo.aspx?pid=" + pic.Id + "' title='" + pic.Desp + "'><img src=" + pic.Pic + " width='120px'></a></li>";
                }
                //初始化按钮
                CommentAction ca = new CommentAction();
                if (Session["user"] == null) Response.Redirect("login.aspx");
                else 
                {
                    string uid = ((User)Session["user"]).Uid;
                    if (uid == pa.showPic(pid).Uid) 
                    {
                        this.Button3.Visible = true;
                        this.Button4.Visible = true;
                    } 
                    else 
                    {
                        this.Button3.Visible = false;
                        this.Button4.Visible = false;
                    }
                    if (ca.isfavor(pid, uid)) 
                    {
                        this.Button2.Visible = true;
                        this.Button1.Visible = false;
                    }
                    else
                    {
                        this.Button1.Visible = true;
                        this.Button2.Visible = false;
                    }
                }
            }
        }
        else
        {
            Response.Redirect("Default.aspx");
        }
    }
    protected void submitcomment_Click(object sender, EventArgs e)
    {
        string pid = Request["pid"].ToString();
        PhotoAction pa = new PhotoAction();
        string sid=pa.showPic(pid).Uid;//图片来源者id
        string uid = ((User)Session["user"]).Uid;//当前用户id
        string content = this.newcomment.Text;
        CommentAction ca = new CommentAction();
        ca.addComent(pid, uid, sid, content);
        Response.Redirect("photo.aspx?pid="+pid);
    }

    //收藏
    protected void Button1_Click(object sender, EventArgs e)
    {
        CommentAction ca = new CommentAction();
        string pid=Request["pid"].ToString();
        ca.addFavor(pid,((User)Session["user"]).Uid);
        
        Response.Redirect("photo.aspx?pid=" + pid);
    }
    //取消收藏
    protected void Button2_Click(object sender, EventArgs e)
    {
        CommentAction ca = new CommentAction();
        string pid = Request["pid"].ToString();
        ca.offFavor(pid, ((User)Session["user"]).Uid);

        Response.Redirect("photo.aspx?pid=" + pid);
    }
    //修改图片
    protected void Button3_Click(object sender, EventArgs e)
    {
        string pid = Request["pid"].ToString();
        Response.Redirect("picupload.aspx?pid="+pid);
    }
    //删除图片
    protected void Button4_Click(object sender, EventArgs e)
    {
        PhotoAction pa = new PhotoAction();
        string pid = Request["pid"].ToString();
        Picture p=pa.showPic(pid);
        if (p.State.Equals("1"))//封面图片不能删
        { 
            Response.Redirect("album.aspx?aid=" + p.Aid); 
        }
        else
        {
            pa.deletePic(pid);
            Response.Redirect("albumshow.aspx?aid=" + p.Aid);
        }
    }
}
